package api

import (
	"server/global"
	"server/model/request"
	"server/model/response"
	"server/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ArticleApi struct{}

// ArticleCreate 新增文章
// @Tags      文章接口
// @Summary   新增文章
// @Description 新增文章
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.ArticleCreate  true  "文章新增成功"
// @Success   200   {object}  response.Response{msg=string}  "文章新增成功"
// @Router    /article/create [post]
func (articleApi *ArticleApi) ArticleCreate(c *gin.Context) {
	var req request.ArticleCreate
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = articleService.ArticleCreate(req)
	if err != nil {
		global.Log.Error("Failed to create article:", zap.Error(err))
		response.FailWithMessage("Failed to create article", c)
		return
	}
	response.OkWithMessage("Successfully created article", c)
}

// ArticleUpdate 更新文章
// @Tags      文章接口
// @Summary   更新文章
// @Description 更新文章
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.ArticleUpdate  true  "文章更新成功"
// @Success   200   {object}  response.Response{msg=string}  "文章更新成功"
// @Router    /article/update [put]
func (articleApi *ArticleApi) ArticleUpdate(c *gin.Context) {
	var req request.ArticleUpdate
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = articleService.ArticleUpdate(req)
	if err != nil {
		global.Log.Error("Failed to update article:", zap.Error(err))
		response.FailWithMessage("Failed to update article", c)
		return
	}
	response.OkWithMessage("Successfully update article", c)
}

// ArticleDelete 删除文章
// @Tags      文章接口
// @Summary   删除文章
// @Description 删除文章
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.ArticleDelete  true  "删除文章成功"
// @Success   200   {object}  response.Response{msg=string}  "删除文章成功"
// @Router    /article/delete [delete]
func (articleApi *ArticleApi) ArticleDelete(c *gin.Context) {
	var req request.ArticleDelete
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = articleService.ArticleDelete(req)
	if err != nil {
		global.Log.Error("Failed to delete article:", zap.Error(err))
		response.FailWithMessage("Failed to delete article", c)
		return
	}
	response.OkWithMessage("Successfully delete article", c)
}

// ArticleList 文章列表
// @Tags      文章接口
// @Summary   文章列表
// @Description 文章列表
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.ArticleList  true  "获取文章列表"
// @Success   200   {object}  response.Response{msg=string}  "获取文章列表"
// @Router    /article/list [get]
func (articleApi *ArticleApi) ArticleList(c *gin.Context) {
	var req request.ArticleList
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := articleService.ArticleList(req)
	if err != nil {
		global.Log.Error("Failed to get article list:", zap.Error(err))
		response.FailWithMessage("Failed to get article list", c)
		return
	}
	response.OkWithData(response.PageResult{
		List:  list,
		Total: total,
	}, c)
}

// ArticleLike 文章收藏/取消收藏
// @Tags      文章接口
// @Summary   文章收藏/取消收藏
// @Description 文章收藏/取消收藏
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.ArticleLike  true  "文章收藏/取消收藏"
// @Success   200   {object}  response.Response{msg=string}  "文章收藏/取消收藏"
// @Router    /article/like [post]
func (articleApi *ArticleApi) ArticleLike(c *gin.Context) {
	var req request.ArticleLike
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	req.UserID = utils.GetUserID(c)
	err = articleService.ArticleLike(req)
	if err != nil {
		global.Log.Error("Failed to complete the operation:", zap.Error(err))
		response.FailWithMessage("Failed to complete the operation", c)
		return
	}
	response.OkWithMessage("Successfully completed the operation", c)
}

// ArticleIsLike 文章收藏状态
// @Tags      文章接口
// @Summary   文章收藏状态
// @Description 文章收藏/取消收藏
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.ArticleLike  true  "文章收藏状态"
// @Success   200   {object}  response.Response{msg=string}  "文章收藏状态"
// @Router    /article/isLike [get]
func (articleApi *ArticleApi) ArticleIsLike(c *gin.Context) {
	var req request.ArticleLike
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	req.UserID = utils.GetUserID(c)
	isLike, err := articleService.ArticleIsLike(req)
	if err != nil {
		global.Log.Error("Failed to get article status:", zap.Error(err))
		response.FailWithMessage("Failed to get article status", c)
		return
	}
	response.OkWithData(isLike, c)
}

func (articleApi *ArticleApi) ArticleLikesList(c *gin.Context) {
	var pageinfo request.ArticleLikesList
	err := c.ShouldBindQuery(&pageinfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	pageinfo.UserID = utils.GetUserID(c)
	list, total, err := articleService.ArticleLikesList(pageinfo)
	if err != nil {
		global.Log.Error("Failed to get likes list:", zap.Error(err))
		response.FailWithMessage("Failed to get likes list", c)
		return
	}
	response.OkWithData(response.PageResult{
		List:  list,
		Total: total,
	}, c)
}
